home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / misc_src / tgraf011 / grafdemo.cpp next >
C/C++ Source or Header  |  1995-11-01  |  8KB  |  248 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <owl.h>
  4. #include <mdi.h>
  5. #include "tgraf.h"
  6.  
  7. // Define user commands
  8. #define CM_PRINT 101
  9. #define CM_ABOUT 102
  10.  
  11. class TM : public TWindow
  12. {
  13. public:
  14.     TGraf* PLOT;
  15.     TM(PTWindowsObject AParent, LPSTR ATitle,PTModule AModule = NULL)
  16.     :TWindow(AParent, ATitle, AModule) {};
  17.     virtual ~TM() { delete PLOT; };
  18.     virtual void SetupWindow(){PLOT = new TGraf(HWindow, 1,1, 100,100);}
  19.     virtual void Paint(HDC PaintDC, PAINTSTRUCT _FAR &PaintInfo);
  20.     virtual void CMPRINT(RTMessage Msg) = [CM_FIRST + CM_PRINT] { PLOT->Print();}
  21.     virtual void CMABOUT(RTMessage Msg) = [CM_FIRST + CM_ABOUT] { PLOT->About();}
  22. };
  23.  
  24. void TM::Paint(HDC PaintDC, PAINTSTRUCT _FAR &PaintInfo)
  25. {
  26.     TWindow::Paint(PaintDC, PaintInfo);
  27.     RECT rc;
  28.     GetClientRect(HWindow, &rc);
  29.     PLOT->Plot(rc);
  30. }
  31.  
  32. class TM2 : public TM
  33. {
  34. public:
  35.     TM2(PTWindowsObject AParent, LPSTR ATitle,PTModule AModule = NULL)
  36.     :TM(AParent, ATitle, AModule) {};
  37.     virtual void SetupWindow();
  38. };
  39.  
  40. void TM2::SetupWindow()
  41. {
  42.     TWindow::SetupWindow();
  43.  
  44.     PLOT = new TGraf(HWindow, 1,1, 100,100);
  45.  
  46.     /*IF you want to see the hidden plot,
  47.       please, comment out the following lines*/
  48.  
  49.     PLOT->New_Plot();            // Erase previous plot
  50.  
  51.     PLOT->Set_Titles("Scatter Plot","", "");// Titles
  52.     PLOT->Set_Scale(-100.0,100.0,-100.0,100);       // User scale (xmin, ymin, xmax, ymax)
  53.     PLOT->Set_Plot_Type(pt_scatter);        // plot type 
  54.     PLOT->Set_X_Label_Format("%10.0f");        // x label format
  55.     PLOT->Set_Y_Label_Format("%10.0f");        // y label format
  56.     PLOT->Set_Tics(10,10,4,4);            // tics (xmajor,xminor,ymajor,yminor)
  57.  
  58.     //* Add some points
  59.  
  60.         PLOT->Set_Line_Style(PS_SOLID);            // change line to solid line
  61.     PLOT->Set_Color(pc_yellow);            // change pen color to yellow
  62.  
  63.     char number[40];                // to hold some string
  64.  
  65.     for (int i = 0; i<=20;i++){
  66.     int random_number_x = rand() % 200 - 100;
  67.     int random_number_y = rand() % 200 - 100;
  68.     sprintf(number,"No.%i", i);
  69.     PLOT->Add_Point(random_number_x,random_number_y,number);
  70.     };
  71. }
  72.  
  73.  
  74. class TM3 : public TM
  75. {
  76. public:
  77.     TM3(PTWindowsObject AParent, LPSTR ATitle,PTModule AModule = NULL)
  78.     :TM(AParent, ATitle, AModule) {};
  79.     virtual void SetupWindow();
  80. };
  81.  
  82. void TM3::SetupWindow()
  83. {
  84.     TWindow::SetupWindow();
  85.  
  86.     PLOT = new TGraf(HWindow, 1,1, 100,100);
  87.  
  88.     /*IF you want to see the hidden plot,
  89.       please, comment out the following lines*/
  90.  
  91.     PLOT->New_Plot();            // Erase previous plot
  92.     PLOT->Set_Titles("Results vs. Time","Time(s)", "Results");// Titles
  93.     PLOT->Set_Scale(-100.0,100.0,-100.0,100);       // User scale (xmin, ymin, xmax, ymax)
  94.     PLOT->Set_Plot_Type(pt_line_dot);        // plot type
  95.     PLOT->Set_X_Label_Format("%10.0f");        // x label format
  96.     PLOT->Set_Y_Label_Format("%10.0f");        // y label format
  97.     PLOT->Set_Tics(10,8,5,5);            // tics (xmajor,xminor,ymajor,yminor)
  98.  
  99.     /********************************************************
  100.      Add some points
  101.          *******************************************************/
  102.  
  103.     PLOT->Set_Color(pc_red);            //change pen color to yellow
  104.  
  105.         // add a point at -80,-80, and attach text "First Data Set"
  106.     PLOT->Add_Point(-80,-80*cos(4*-100*3.141567/180),"1st Data Set");
  107.  
  108.         // add points from x=-95 to 5 and _DO NOT_ attach text
  109.     for(double i=-75;i<=-5;i+=5) PLOT->Add_Point(i,i*cos(4*i*3.141567/180));
  110.  
  111.     //-------------
  112.         //New set;
  113.     PLOT->New_Set();
  114.     PLOT->Set_Color(pc_blue);            // change pen color to blue
  115.     PLOT->Set_Line_Style(PS_DASH);            // change line type to Dash
  116.  
  117.     // add a point at 0,0, and add text "2nd Data Set"
  118.     PLOT->Add_Point(0,-20 + 0*cos(4*0*3.141567/180),"2nd Data Set");
  119.  
  120.         // add some more points from x=5 to 95
  121.     for(i=5;i<=95;i+=5) PLOT->Add_Point(i,-20 + i*cos(4*i*3.141567/180));
  122.  
  123.         //-------------
  124.     PLOT->New_Set();
  125.         PLOT->Set_Color(pc_yellow);            // change pen color to green
  126.     PLOT->Set_Line_Style(PS_DOT);            // change line type to Dot
  127.  
  128.     // add the last point at 50,50, and attach text "Last Point"
  129.     PLOT->Add_Point(50,50*cos(4*100*3.141567/180),"3rd Data Set");
  130.     for(i=55;i<=100;i+=5) PLOT->Add_Point(i,10 + i*sin(3*i*3.141567/180));
  131. }
  132.  
  133. class TM4 : public TM
  134. {
  135. public:
  136.     TM4(PTWindowsObject AParent, LPSTR ATitle,PTModule AModule = NULL)
  137.     :TM(AParent, ATitle, AModule) {};
  138.     virtual void SetupWindow();
  139. };
  140.  
  141. void TM4::SetupWindow()
  142. {
  143.     TWindow::SetupWindow();
  144.  
  145.     PLOT = new TGraf(HWindow, 1,1, 100,100);
  146.  
  147.     /*IF you want to see the hidden plot,
  148.       please, comment out the following lines*/
  149.  
  150.     PLOT->New_Plot();            // Erase previous plot
  151.     PLOT->Set_Titles("Some Lines","", "Line Styles");// Titles
  152.     PLOT->Set_Scale(-100.0,100.0,-100.0,100);       // User scale (xmin, ymin, xmax, ymax)
  153.     PLOT->Set_Plot_Type(pt_line);        // plot type
  154.     PLOT->Set_X_Label_Format("%10.0f");        // x label format
  155.     PLOT->Set_Y_Label_Format("%10.0f");        // y label format
  156.     PLOT->Set_Tics(8,8,4,4);            // tics (xmajor,xminor,ymajor,yminor)
  157.  
  158.     /********************************************************
  159.      Add some points
  160.          *******************************************************/
  161.  
  162.     PLOT->Set_Color(pc_red);            //change pen color to yellow
  163.     PLOT->Set_Line_Style(PS_SOLID);
  164.     // add points from x=-100 to 100 and _DO NOT_ attach text
  165.     for(double i=-100;i<=100;i+=10) PLOT->Add_Point(i,10*cos(4*i*3.141567/180)+90);
  166.     //-------------
  167.         //New set;
  168.     PLOT->New_Set();
  169.     PLOT->Set_Color(pc_blue);                   // change pen color to blue
  170.     PLOT->Set_Line_Style(PS_DOT);        
  171.     for(i=-100;i<=100;i+=10) PLOT->Add_Point(i,20*sin(4*i*3.141567/180)+50);
  172.     //-------------
  173.     PLOT->New_Set();
  174.         PLOT->Set_Color(pc_yellow);            // change pen color to green
  175.     PLOT->Set_Line_Style(PS_DASHDOT);
  176.     for(i=-100;i<=100;i+=10) PLOT->Add_Point(i,30*cos(4*i*3.141567/180)+0);
  177.     //-------------
  178.     PLOT->New_Set();
  179.         PLOT->Set_Color(pc_magenta);            // change pen color to green
  180.     PLOT->Set_Line_Style(PS_DASH);
  181.     for(i=-100;i<=100;i+=10) PLOT->Add_Point(i,20*sin(4*i*3.141567/180)-50);
  182.     //-------------
  183.     PLOT->New_Set();
  184.     PLOT->Set_Color(pc_cyan);            // change pen color to green
  185.     PLOT->Set_Line_Style(PS_DASHDOTDOT);
  186.     for(i=-100;i<=100;i+=10) PLOT->Add_Point(i,10*cos(4*i*3.141567/180)-90);
  187. }
  188.  
  189. // Define a TApplication descendant
  190. class TMDIApp : public TApplication {
  191. public:
  192.     TMDIApp(LPSTR name, HINSTANCE hInstance,
  193.           HINSTANCE hPrevInstance, LPSTR lpCmd,
  194.           int nCmdShow)
  195.             : TApplication(name, hInstance,
  196.                    hPrevInstance, lpCmd, nCmdShow) {};
  197.     virtual void InitMainWindow();
  198. };
  199.  
  200. // Define a TMDIFrame descendant
  201. class TMyMDIFrame : public TMDIFrame {
  202. public:
  203.   WORD ChildNum;
  204.  
  205.   TMyMDIFrame(LPSTR ATitle, LPSTR MenuName);
  206.   virtual void SetupWindow();
  207.   virtual PTWindowsObject InitChild();
  208. };
  209.  
  210. // Construct the TMDIApp's MainWindow object, loading its menu
  211. void TMDIApp::InitMainWindow()
  212. {
  213.   MainWindow = new TMyMDIFrame("TGraf Demo Program","TGRAFDEMOMENU");
  214. }
  215.  
  216. TMyMDIFrame::TMyMDIFrame(LPSTR ATitle, LPSTR MenuName)
  217.              : TMDIFrame(ATitle, MenuName)
  218. {
  219.   ChildNum = 1;
  220. }
  221.  
  222. void TMyMDIFrame::SetupWindow()
  223. {
  224.   TMDIFrame::SetupWindow();
  225.   GetApplication()->MakeWindow(new TM4(this,"TGraf User 3"));
  226.   GetApplication()->MakeWindow(new TM3(this,"TGraf User 2"));
  227.   GetApplication()->MakeWindow(new TM2(this,"TGraf User 1"));
  228.   GetApplication()->MakeWindow(new TM(this,"TGraf"));
  229. }
  230.  
  231. PTWindowsObject TMyMDIFrame::InitChild()
  232. {
  233.   char ChildName[14];
  234.   wsprintf(ChildName,"TGraf", ChildNum++);
  235.   return new TM(this, ChildName);
  236. }
  237.  
  238. // Run the MDIApp
  239. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  240.            LPSTR lpCmd, int nCmdShow)
  241. {
  242.     TMDIApp MDIApp ("MDIApp", hInstance, hPrevInstance,
  243.         lpCmd, nCmdShow);
  244.     MDIApp.Run();
  245.     return (MDIApp.Status);
  246. }
  247.  
  248.